package web

import (
	"gitee.com/Hu-Lyndon/distill-resk/infra/api"
	"gitee.com/Hu-Lyndon/distill-resk/infra/authentication/middleware/jwts"
	"gitee.com/Hu-Lyndon/distill-resk/infra/authentication/middleware/jwts/models"
	"gitee.com/Hu-Lyndon/distill-resk/infra/authentication/vo"
	"gitee.com/Hu-Lyndon/distill-resk/infra/utils/supports"
	"gitee.com/Hu-Lyndon/distill-resk/infra/web"
	"github.com/kataras/iris"
	"github.com/kataras/iris/hero"
)

func init() {
	api.RegisterApi(new(UserApi))
}

type UserApi struct {
}

func (u *UserApi) Init() {
	groupRouter := web.IrisMainParty()
	HomeHub(groupRouter)
	UserHub(groupRouter)
}

func (u *UserApi) Setup() {

}

func HomeHub(party iris.Party) {
	home := party.Party("/")
	home.Get("/", func(ctx iris.Context) { // 首页模块
		ctx.View("index.html")
	})
	home.Get("/sysMenu", hero.Handler(DynamicMenu)) // 获取动态菜单
}

func DynamicMenu(ctx iris.Context) {

	var (
		user         *models.UserApp
		isParseToken bool
		menuList     []*models.Menu
	)

	if user, isParseToken = jwts.ParseToken(ctx); !isParseToken {
		return
	}

	// 获取用户的菜单列表
	menuList = models.DynamicMenuTree(int64(user.UserId))
	supports.Ok(ctx, supports.OptionSuccess, vo.BuildMenuTree(menuList))
}
